package org.social.manager;

import org.constant.smallkey.SocialDiscreteDataSmallKey;
import org.social.db.bean.SocialDiscreteData;
import org.social.db.cache.SocialDiscreteDataCache;

import com.sojoys.artifact.constant.ServerStatusEnum;
import com.sojoys.artifact.core.IServer;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * 服务器维护状态管理器
 * 
 * @author liu.jiang
 * @date 2018年4月19日
 */
public class ServerStatusManager {

    static final Log log = LogFactory.get(ServerStatusManager.class);

    private ServerStatusManager() {}

    private static ServerStatusManager ME = new ServerStatusManager();

    public static ServerStatusManager me() {
        return ME;
    }

    /**
     * 获取服务器维护
     * 
     * @return
     */
    public int getServerStatus() {
        int status = ServerStatusEnum.AEGIS.getCode();// 正式服默认维护中
        if (IServer.getConstants().isDevMode()) {
            status = ServerStatusEnum.RUNNING.getCode();// 开发模式默认运行
        }
        SocialDiscreteData socialDiscreteData =
                SocialDiscreteDataCache.me().getByType(SocialDiscreteDataSmallKey.SERVER_STATUS);
        if (socialDiscreteData == null) {
            socialDiscreteData =
                    new SocialDiscreteData(0, SocialDiscreteDataSmallKey.SERVER_STATUS, status);
            socialDiscreteData.insert();
        } else {
            status = (Integer) socialDiscreteData.getData();
        }
        return status;
    }

    /**
     * 更新服务器维护状态
     */
    public void updateServerStatus(int status) {
        SocialDiscreteData socialDiscreteData =
                SocialDiscreteDataCache.me().getByType(SocialDiscreteDataSmallKey.SERVER_STATUS);
        if (socialDiscreteData == null) {
            socialDiscreteData =
                    new SocialDiscreteData(0, SocialDiscreteDataSmallKey.SERVER_STATUS, status);
            socialDiscreteData.insert();
        } else {
            socialDiscreteData.setData(status);
            socialDiscreteData.update();
        }
        log.info("-----updateServerStatus = " + status);
    }
}
